package com.hr.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hr.domain.dto.AddHouseImageDTO;
import com.hr.domain.po.HouseImage;
import com.hr.domain.query.HouseImagePageParams;
import com.hr.domain.query.HouseImagePageResult;
import com.hr.domain.vo.HouseImageVO;
import com.hr.service.HouseImageService;
import com.hr.mapper.HouseImageMapper;
import com.hr.utils.AliyunOSSProperties;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Stream;

/**
* @author 86150
* @description 针对表【house_image(房源图片表)】的数据库操作Service实现
* @createDate 2025-09-24 14:18:36
*/
@Slf4j
@Service
@AllArgsConstructor
public class HouseImageServiceImpl implements HouseImageService{

    private final HouseImageMapper houseImageMapper;

    private final AliyunOSSProperties aliyunOSSProperties;
    /**
     * 分页获取房屋图片列表
     * @return houseImageList
     */
    @Override
    public HouseImagePageResult getHouseImagePageList(HouseImagePageParams queryParams) {
        //开始分页
        PageHelper.startPage(queryParams.getPageNum(), queryParams.getPageSize());
        //传入分页参数
        List<HouseImageVO> houseImageList = houseImageMapper.getHouseImagePageList(queryParams);
        //强转为Page<HouseImageVO>
        Page<HouseImageVO> page = (Page<HouseImageVO>) houseImageList;
        //封装为HouseImagePageResult，返回
        return new HouseImagePageResult(page.getTotal(), page.getResult());
    }

    /**
     * 添加房屋图片
     * @param addHouseImageDTO
     * @return msg
     */
    @Override
    public String addHouseImage(AddHouseImageDTO addHouseImageDTO) {
        HouseImage houseImage = new HouseImage();
        BeanUtils.copyProperties(addHouseImageDTO, houseImage);

        //首先判断插入的sortOrder是否为空和是否重复
        if (houseImage.getSortOrder() == null ){
            return "请传入排序序号";
        }
        List<Integer> sortOrder = houseImageMapper.getSortOrder(houseImage.getHouseId());

        for (Integer sortOrder1 : sortOrder) {
            if (sortOrder1.equals(houseImage.getSortOrder())){
                return "排序序号重复";
            }
        }

        int row = houseImageMapper.addHouseImage(houseImage);

        return row > 0 ? "添加成功" : "添加失败";


    }

    @Override
    public List<Integer> getSortOrder(Long houseId) {

        List<Integer> list = houseImageMapper.getSortOrder(houseId).stream()
                .filter(sortOrder -> sortOrder != null)
                .toList();

        return list;
    }

    /**
     * 删除房屋图片
     * @param id
     * @return msg
     */
    @Override
    public String deleteHouseImage(Integer id) {
        int row = houseImageMapper.deleteHouseImage(id);
        return row > 0 ? "删除成功" : "删除失败";
    }

    /**
     * 批量删除房屋图片
     * @param ids
     * @return msg
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String deleteHouseImageBatch(List<Integer> ids) {
        if (ids == null || ids.isEmpty()){
            return "请传入要删除的图片id";
        }
        try {
            int deletedRows = houseImageMapper.deleteHouseImageBatch(ids);
            if (deletedRows != ids.size()) {
                log.error("批量删除不完全，期望删除 {} 条，实际删除 {} 条", ids.size(), deletedRows);
                return "部分删除失败";
            }
            return "批量删除成功";


        }catch (Exception e){
            log.error("批量删除异常", e);
            throw new RuntimeException("删除失败", e);
        }

    }
}




